// pages/mapFindHouse/mapFindHouse.js
const citySelector = requirePlugin('citySelector');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: '',
    longitude:'',
    markers:[],
    typeId:1,
    distance:'DISTANCE_5',
    price:'PRICE_4',
    priceSelect:{},
    distanceSelect:{},
    city:"北京"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {
    const selectedCity = citySelector.getCity(); // 选择城市后返回城市信息对象，若未选择返回null 默认为北京
    if (selectedCity!=null) {
      this.setData({
        city:selectedCity.name
      })
    }else{
      this.setData({
        city:'北京'
      })
    }
    
  },

 
  onLoad(options) {

    // 价格筛选
    let price = [
      '0-999',
      '1000-9999',
      '10000-99999',
      '不限'
    ];
    const priceSelectOptions =new Array(price.length).fill(null).map((_, i) => ({
      label: `${price[i]}`,
      value: `PRICE_${i + 1}`,
      disabled: false,
    }));
    this.setData({
      priceSelect:priceSelectOptions
    })
    // 距离筛选
    let distance=[
      '1000米内',
      '2000米内',
      '3000米内',
      '5000米内',
      '不限'
    ]
    const distanceSelect =new Array(distance.length).fill(null).map((_, i) => ({
      label: `${distance[i]}`,
      value: `DISTANCE_${i + 1}`,
      disabled: false,
    }));
  
    this.setData({
      distanceSelect:distanceSelect
    })
    wx.getLocation({
      altitude: 'altitude',
      success:res=>{
        this.setData({
            latitude:res.latitude,
            longitude:res.longitude
        })
        this.getNearByHouse(res.latitude,res.longitude,this.data.distance,this.data.typeId,this.data.price)
      }
    })
  },

  /**
   * 选择城市
   */
  chooseCity(){
    const key = 'RNOBZ-EQR6R-UN6W5-WDY6A-FEJI6-2MBIV'; // 使用在腾讯位置服务申请的key
    const referer = 'map'; // 调用插件的app的名称
    const hotCitys = ''; // 用户自定义的的热门城市

    wx.navigateTo({
    url: `plugin://citySelector/index?key=${key}&referer=${referer}&hotCitys=${hotCitys}`,
    })
  },


  /**
   * 获取附近房源
   */
  getNearByHouse(latitude,longitude,distance=this.data.distance,type=this.data.type,price=this.data.price){
    wx.request({
      url: 'http://www.house.com/index.php/api/nearby_house',
      data:{
        latitude,
        longitude,
        distance,
        type,
        price
      },
      success:res=>{
        if (res.data.code==200) {
            this.setData({
                markers:res.data.data
            })
        }else{
          this.setData({
            markers:[]
          })
        }
      }
    })
  },

  /**
   * 选择租房买房类型
   * @param {*} e 
   */
  clickType(e){
    let typeId = e.currentTarget.dataset.typeid
    this.setData({
        typeId
    })
  
    let latitude = this.data.latitude;
    let longitude = this.data.longitude;
    let distance = this.data.distance;
    let type = this.data.typeId
    console.log(type)
    let price = this.data.price
    this.getNearByHouse(latitude,longitude,distance,type,price)
  },

  /**
   * 点击marker点获取id查找房源详情
   * @param {*} e 
   */
  mapHouse(e){
    let houseId = e.detail.markerId
    wx.navigateTo({
      url: '/pages/payHouseDeatil/payHouseDeatil?detail_id='+houseId,
    })
  },



  /**
   * 价格筛选
   * @param {*} e 
   */
  priceSingleSelect(e){
    this.setData({
      price:e.detail.value
    })
    let latitude = this.data.latitude;
    let longitude = this.data.longitude;
    let distance = this.data.distance;
    let type = this.data.typeId
    let price = this.data.price
    this.getNearByHouse(latitude,longitude,distance,type,price)
  },
  /**
   * 距离筛选
   */
  distanceSingleSelect(e){
    this.setData({
      distance:e.detail.value
    })
    let latitude = this.data.latitude;
    let longitude = this.data.longitude;
    let distance = this.data.distance;
    let type = this.data.typeId
    let price = this.data.price
    this.getNearByHouse(latitude,longitude,distance,type,price)
  }
})